package org.exeed.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.exeed.bean.ResultBean;
import org.exeed.dao.*;
import org.exeed.entity.ImageEntity;
import org.exeed.entity.PdfEntity;
import org.exeed.entity.SysEntity;
import org.exeed.entity.VideoEntity;
import org.exeed.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Service
public class ResourceService {

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private SysMapper sysMapper;

    @Autowired
    private PdfMapper pdfMapper;

    @Autowired
    private SysService sysService;

    @Value("${file.path}")
    private String filepath = "./temp/";

    @Value("${file.tmp}")
    private String tmpPath = "";
    private static String VIDEO_COVER_PATH = "video/cover/";

    private static String VIDEO_PATH = "video/";

    private static String IMAGE_PATH = "image/";

    private static String PDF_PATH = "pdf/";


    public void saveVideo(VideoEntity videoEntity) {

        String url = getResourceUrl();//静态文件路径

        if (videoEntity.getCoverUrl() != null && !videoEntity.getCoverUrl().contains(sysService.getDefaultUrl())) {
            File file = new File(tmpPath + videoEntity.getCoverUrl());
            if (file.exists() && file.isFile()) {
                videoEntity.setCoverFileName(videoEntity.getCoverUrl());
                FileUtil.copy(tmpPath + videoEntity.getCoverUrl(), filepath + VIDEO_COVER_PATH + videoEntity.getCoverUrl(), true);
                videoEntity.setCoverUrl(url + VIDEO_COVER_PATH + videoEntity.getCoverUrl());
            }
        }

        if (videoEntity.getResourceUrl() != null &&!videoEntity.getResourceUrl().contains(sysService.getDefaultUrl())) {
            File file = new File(tmpPath + videoEntity.getResourceUrl());
            if (file.exists() && file.isFile()) {
                videoEntity.setFileName(videoEntity.getResourceUrl());
                FileUtil.copy(tmpPath + videoEntity.getResourceUrl(), filepath + VIDEO_PATH + videoEntity.getResourceUrl(), true);
                videoEntity.setResourceUrl(url + VIDEO_PATH + videoEntity.getResourceUrl());
            }
        }

        videoEntity.setUpdateTime(DateUtil.current());

        if (videoEntity.getId() == null || videoEntity.getId() == 0) {
            videoEntity.setId(JWTUtils.createId());
            videoMapper.insert(videoEntity);
        } else {
            videoMapper.updateById(videoEntity);
        }

    }

    public Map selectVideoList(Integer page, Integer size, String key) {

        Page<VideoEntity> iPage = new Page<>(page, size);
        QueryWrapper<VideoEntity> queryWrapper = new QueryWrapper<>();
        if (key != null && !key.equals("")) {
            queryWrapper.like("title", key);
            queryWrapper.orderByDesc("publish_time");
        }
        queryWrapper.orderByDesc("update_time");
        IPage<VideoEntity> list = videoMapper.selectPage(iPage, queryWrapper);
        Map<String, Object> R = new HashMap<>();
        R.put("page", page);
        R.put("size", size);
        R.put("total", list.getTotal());
        R.put("list", list.getRecords());
        return R;
    }

    public VideoEntity selectVideoById(Long id) {
        return videoMapper.selectById(id);
    }

    public void deleteVideoById(Long id) {
        videoMapper.deleteById(id);
    }

    public void saveImage(ImageEntity entity) {
        String url = getResourceUrl();

        if (entity.getResourceUrl() != null && !entity.getResourceUrl().contains(sysService.getDefaultUrl())) {
            File file = new File(tmpPath + entity.getResourceUrl());
            if (file.exists() && file.isFile()) {
                entity.setFileName(entity.getResourceUrl());
                FileUtil.copy(tmpPath + entity.getResourceUrl(), filepath + IMAGE_PATH + entity.getResourceUrl(), true);
                entity.setResourceUrl(url + IMAGE_PATH + entity.getResourceUrl());
            }
        }
        if (entity.getHqResourceUrl() != null && !entity.getHqResourceUrl().contains(sysService.getDefaultUrl())) {
            File file = new File(tmpPath + entity.getHqResourceUrl());
            if (file.exists() && file.isFile()) {
                entity.setFileName(entity.getHqResourceUrl());
                FileUtil.copy(tmpPath + entity.getHqResourceUrl(), filepath + IMAGE_PATH + entity.getHqResourceUrl(), true);
                entity.setHqResourceUrl(url + IMAGE_PATH + entity.getHqResourceUrl());
            }
        }

        entity.setUpdateTime(DateUtil.current());

        if (entity.getId() == null || entity.getId() == 0) {
            entity.setId(JWTUtils.createId());
            imageMapper.insert(entity);
        } else {
            imageMapper.updateById(entity);
        }

    }

    public Map selectImageList(Integer page, Integer size, String key) {

        Page<ImageEntity> iPage = new Page<>(page, size);
        QueryWrapper<ImageEntity> queryWrapper = new QueryWrapper<>();
        if (key != null && !key.equals("")) {
            queryWrapper.like("title", key);
            queryWrapper.orderByDesc("publish_time");
        }
        queryWrapper.orderByDesc("update_time");
        IPage<ImageEntity> list = imageMapper.selectPage(iPage, queryWrapper);
        Map<String, Object> R = new HashMap<>();
        R.put("page", page);
        R.put("size", size);
        R.put("total", list.getTotal());
        R.put("list", list.getRecords());
        return R;
    }

    public ImageEntity selectImageById(Long id) {
        return imageMapper.selectById(id);
    }


    public void deleteImageById(Long id) {
        imageMapper.deleteById(id);
    }


    public void savePdf(PdfEntity entity) {
        String url = getResourceUrl();

        if ( entity.getResourceUrl() != null && !entity.getResourceUrl().contains(sysService.getDefaultUrl())) {
            File file = new File(tmpPath + entity.getResourceUrl());
            if (file.exists() && file.isFile()) {
                entity.setFileName(entity.getResourceUrl());
                FileUtil.copy(tmpPath + entity.getResourceUrl(), filepath + PDF_PATH + entity.getResourceUrl(), true);
                entity.setResourceUrl(url + PDF_PATH + entity.getResourceUrl());
            }
        }
        entity.setUpdateTime(DateUtil.current());

        if (entity.getId() == null || entity.getId() == 0) {
            entity.setId(JWTUtils.createId());
            pdfMapper.insert(entity);
        } else {
            pdfMapper.updateById(entity);
        }

    }

    public Map selectPdfList(Integer page, Integer size, String key) {

        Page<PdfEntity> iPage = new Page<>(page, size);
        QueryWrapper<PdfEntity> queryWrapper = new QueryWrapper<>();
        if (key != null && !key.equals("")) {
            queryWrapper.like("title", key);
            queryWrapper.orderByDesc("publish_time");
        }
        queryWrapper.orderByDesc("update_time");
        IPage<PdfEntity> list = pdfMapper.selectPage(iPage, queryWrapper);
        Map<String, Object> R = new HashMap<>();
        R.put("page", page);
        R.put("size", size);
        R.put("total", list.getTotal());
        R.put("list", list.getRecords());
        return R;
    }

    public PdfEntity selectPdfById(Long id) {
        return pdfMapper.selectById(id);
    }

    public void deletePdfById(Long id) {
        pdfMapper.deleteById(id);
    }

    public String getResourceUrl() {
        return sysMapper.selectOne(new QueryWrapper()).getResourceUrl();
    }

}
